package lex;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

import wordnet.WordNet;
import constants.Constants;

public class Lexicon {

	private HashMap<String, Double> names;	// ranked names
	private HashMap<String, Boolean> geos;	// geography names
	private HashMap<String, Boolean> orgs; //organization names
	private HashMap<String, Boolean> stops;	// stop word list
	private WordNet wordNet;	// instance of the wordnet database
	
	public Lexicon(WordNet wordNet) {
		this.wordNet = wordNet;
		names = new HashMap<String, Double>();
		geos = new HashMap<String, Boolean>();
		orgs = new HashMap<String, Boolean>();
		stops = new HashMap<String, Boolean>();
	}
	
	public void init() {
		
		try{
			// First initialize the names
			System.out.println("System startup");
			System.out.println("Initializing name lexicon... This may take a while");
			FileInputStream fstream = new FileInputStream(Constants.NAMES_LEXICON_PATH);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			Double lineCounter = 1.0;
			while ((strLine = br.readLine()) != null)   {
				names.put(strLine, lineCounter);
				lineCounter+=1.0;
			}
			// set max entries in lexicon
			names.put(Constants.MAX_NAME_LINES, lineCounter);
			System.out.println("--> Done");
			
			in.close();
			System.out.println("Initializing geo lexicon... This may take a while");
			
			// Now initialize the geography lexicon
			fstream = new FileInputStream(Constants.GEO_LEXICON_PATH);
			in = new DataInputStream(fstream);
			br = new BufferedReader(new InputStreamReader(in));

			while ((strLine = br.readLine()) != null)   {
				geos.put(strLine, true);
			}
			
			System.out.println("--> Done");
			
			in.close();
			
			// Now initialize the organization lexicon
			System.out.println("Initializing org lexicon... This may take a while");
			fstream = new FileInputStream(Constants.ORG_LEXICON_PATH);

			in = new DataInputStream(fstream);
			br = new BufferedReader(new InputStreamReader(in));
			
			while ((strLine = br.readLine()) != null)   {
				orgs.put(strLine, true);
			}
			
			System.out.println("--> Done");
			
			in.close();
			
			// Finally create our stop word list
			System.out.println("Initializing stop word list... This may take a while");
			
			// Now initialize the stop word list
			fstream = new FileInputStream(Constants.STOP_LIST_PATH);
			in = new DataInputStream(fstream);
			br = new BufferedReader(new InputStreamReader(in));

			while ((strLine = br.readLine()) != null)   {
				stops.put(strLine, true);
			}
			
			System.out.println("--> Done");
			in.close();
			
		}catch (Exception e){	
			System.err.println("Error: " + e.getMessage());
		}
		
		System.out.println("--> Finished initialization successfully");
	}
	
	public Double getEntries() {
		return names.get(Constants.MAX_NAME_LINES);
	}
	
	public Double isName(String name) {
		Double val = names.get(name.toLowerCase());
		if(val != null) {
			return val;
		}
		else {
			return null;
		}
	}
	
	public boolean isLocation(String location) {
		Boolean val = geos.get(location.toLowerCase());
		if(val != null) {
			return val;
		}
		else {
			return false;
		}
	}
	
	public boolean isOrganization(String organization) {
		// TODO: implement!!!
		String input = organization.toLowerCase();
		if(orgs.get(input) != null){
			return true;
		}
		return false;
	}
	
	public boolean isStopWord(String word) {
		String input = word.toLowerCase();
		// check if exactly this word is a stop word
		if(stops.get(input) != null) {
			return true;
		}
		// TODO check this method...
		/**
		// check every stop list entry for similarity
		for(String cmp : stops.keySet()) {
			if(wordNet.hasSameMeaning(input, cmp)) {
				return true;
			}
		}
		**/
		// no similar term found -> return false
		return false;
	}
	
}
